package com.charger.PromoteOperations.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.charger.PromoteOperations.domain.GroupRule;
import com.charger.PromoteOperations.domain.Rulesparticipation;
import com.charger.PromoteOperations.service.GroupRuleService;
import com.charger.PromoteOperations.service.RulesparticipationService;
import com.charger.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.charger.common.annotation.Log;
import com.charger.common.core.controller.BaseController;
import com.charger.common.core.domain.AjaxResult;
import com.charger.common.enums.BusinessType;
import com.charger.PromoteOperations.domain.Usergroup;
import com.charger.PromoteOperations.service.IUsergroupService;
import com.charger.common.utils.poi.ExcelUtil;
import com.charger.common.core.page.TableDataInfo;

/**
 * 组团管理Controller
 * 
 * @author ruoyi
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/usergroup/usergroup")
public class UsergroupController extends BaseController
{
    @Autowired
    private IUsergroupService usergroupService;
    @Autowired
    private GroupRuleService groupRuleService;
    @Autowired
    private RulesparticipationService rulesparticipationService;

//    @RequestMapping("/test")
//    @Anonymous
//    public String test(){
//        return "test";
//    }
    /**
     * 查询组团管理列表
     */
    @PreAuthorize("@ss.hasPermi('usergroup:usergroup:list')")
    @GetMapping("/list")
    public TableDataInfo list(Usergroup usergroup)
    {
        startPage();
        List<Usergroup> list = usergroupService.selectUsergroupList(usergroup);
        for (Usergroup grouplist : list){
            List<Rulesparticipation> ruleList=new ArrayList<>();
            Long id = grouplist.getId();
            QueryWrapper<GroupRule> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("group_id",id);
            List<GroupRule> groupRules = groupRuleService.list(queryWrapper);
            for (GroupRule groupRule : groupRules){
                Long ruleId = groupRule.getRuleId();
                Rulesparticipation byId = rulesparticipationService.getById(ruleId);
                ruleList.add(byId);
            }
            grouplist.setRulesparticipations(ruleList);
        }
        return getDataTable(list);
    }

    /**
     * 导出组团管理列表
     */
    @PreAuthorize("@ss.hasPermi('usergroup:usergroup:export')")
    @Log(title = "组团管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Usergroup usergroup)
    {
        List<Usergroup> list = usergroupService.selectUsergroupList(usergroup);
        ExcelUtil<Usergroup> util = new ExcelUtil<Usergroup>(Usergroup.class);
        util.exportExcel(response, list, "组团管理数据");
    }

    /**
     * 获取组团管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('usergroup:usergroup:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(usergroupService.selectUsergroupById(id));
    }

    /**
     * 新增组团管理
     */
    @PreAuthorize("@ss.hasPermi('usergroup:usergroup:add')")
    @Log(title = "组团管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Usergroup usergroup)
    {
        List<Rulesparticipation> rulesparticipations = usergroup.getRulesparticipations();
        for (Rulesparticipation rulesparticipation : rulesparticipations){
            GroupRule groupRule = new GroupRule();
            groupRule.setGroupId(usergroup.getId());
            groupRule.setRuleId(rulesparticipation.getId());
            groupRuleService.save(groupRule);
        }
        return toAjax(usergroupService.insertUsergroup(usergroup));
    }

    /**
     * 修改组团管理
     */
    @PreAuthorize("@ss.hasPermi('usergroup:usergroup:edit')")
    @Log(title = "组团管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Usergroup usergroup)
    {
        return toAjax(usergroupService.updateUsergroup(usergroup));
    }

    /**
     * 删除组团管理
     */
    @PreAuthorize("@ss.hasPermi('usergroup:usergroup:remove')")
    @Log(title = "组团管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(usergroupService.deleteUsergroupByIds(ids));
    }
}
